package wfb.frontend;

import wfb.definitions.WfbDbInterface;
import wfb.frontend.components.FactsComponent;
import wfb.frontend.components.HqlQueryComponent;
import wfb.frontend.components.NavComponent;
import wfb.frontend.components.SqlQueryComponent;

import javax.swing.*;
import java.awt.*;
import java.io.FileInputStream;
import java.util.Properties;

/**
 * Main application for viewing world factbook data using middleware to
 * access the data.<p>
 * Changelog
 * <dl>
 *  <li>modified Kommentar-field to use text instead of html</li>
 * </dl>
 */
public class Wfb extends JPanel {
	Properties prop = new Properties();
	/*
	 * wdi enthaelt WfbDbImpl (World Fact Book Data Base Implementation) mit deren
	 * Hilfe mit DB kommuniziert wird
	 */
  WfbDbInterface wdi;
  public Wfb(String propertiesFile, JFrame window) {
  	super();
    ImageIcon icon;
    //  Properties - File laden
    try { 
    	prop.load(new FileInputStream(propertiesFile)); 
    }
    catch ( Exception e ) { 
    	System.err.println("2 Konnte Properties nicht laden : " + e ); System.exit(2); 
    }
    String flaggenIcon = prop.getProperty("flagge");
    String kartenIcon = prop.getProperty("karte");
    String value  = prop.getProperty("modul");
    // Neue WfbDbImpl - Objekt anlegen, ueber welches ich mit DB kommuniziere
    try  {
    	wdi = (WfbDbInterface) Class.forName(value).newInstance();
      wdi.init(prop);
    } 
    catch ( Exception e ){ 
    	System.err.println("3 Konnte Klasse net laden : " + e ); System.exit(3); 
    }
    //System.out.println("flagge = " + flaggenIcon + " && karte = " + kartenIcon + " && klasse = " + value);
    window.addWindowListener(new java.awt.event.WindowAdapter() {
		public void windowClosing(java.awt.event.WindowEvent evt) {
	   	wdi.finish();
     	System.exit(0);
    	}
    });
    // created tabbed pane
    // element0 = aufgabe1 -> SQL Abfrage
    // element1 = aufgabe2 -> Facts about the world
    // element2 = aufgabe3 -> Country-Characteristic Navigator
    JTabbedPane tabbedPane = new JTabbedPane();
		Component panel1 = new SqlQueryComponent(wdi, prop.getProperty("queryThread"));
    icon = new ImageIcon(prop.getProperty("icons")+"/frage.gif");
    tabbedPane.addTab("SQL", icon, panel1, "Shows SQL-Queries in table form");
    tabbedPane.setSelectedIndex(0);
		Component panel2 = new HqlQueryComponent(wdi, prop.getProperty("queryThread"));
		icon = new ImageIcon(prop.getProperty("icons")+"/frage.gif");
		tabbedPane.addTab("HQL", icon, panel2, "Shows HQL-Queries in table form");
		tabbedPane.setSelectedIndex(0);
    //Component panel3 = Aufgabe2(flaggenIcon, kartenIcon);
    JComponent facts = new FactsComponent(wdi,prop,kartenIcon,flaggenIcon);
    facts.setPreferredSize(new Dimension(400,600));
		Component panel3 = new JScrollPane(facts);
    icon = new ImageIcon(prop.getProperty("icons")+"/erde.gif");
    tabbedPane.addTab("Facts", icon, panel3, "View facts about countries");
    Component panel4 = new NavComponent(prop,wdi);
    icon = new ImageIcon(prop.getProperty("icons")+"/rainbow.gif");
    tabbedPane.addTab("Country Navigator", icon, panel4, "Countries by characteristics");
    // tabbed pane created
    // Add the tabbed pane to this panel.
    setLayout(new GridLayout(1, 1));
  	add(tabbedPane);
  	// tabbed pane added
  }
	public static void main(String[] args) {
  	String propertiesFile = "impl.properties";
    if ( args.length == 1 ) { propertiesFile = args[0]; }
    else if  ( args.length > 1 ) { System.err.println("8 ung�ltige Syntax: " +
    	   "nur ein Programmparameter = Position der Property-Datei"); System.exit(8); }
    // Create the Window
    JFrame frame = new JFrame("The World-Fact-Book Applet");
    System.out.println("frame created");
    frame.getContentPane().add(new Wfb(propertiesFile, frame),BorderLayout.CENTER);
    System.out.println("added properties");
    ImageIcon icn = new ImageIcon("wfb/frontend/figures/wand.gif");
    frame.setIconImage(icn.getImage());
    frame.setSize(640, 800);
    System.out.println("setSize");
    frame.setVisible(true);
    System.out.println("setVisible");
	}
}